<?php

namespace Budget\BudgetBundle\Model\Charts;

use Budget\BudgetBundle\Helper\DateHelper;
use Budget\BudgetBundle\Helper\FamilyHelper;
use Budget\BudgetBundle\Entity\Family;
use Doctrine\ORM\EntityManager;

class ChartIncomes extends ChartAbstract 
{
    protected $family;
    
    public function __construct(EntityManager $entityManager, Family $family) 
    {
        parent::__construct($entityManager);
        $this->family = $family;
    }
    
    public function getToDataJson($incomes, $dateTo, $dateFrom)
    {       
        $months = $response = array();
        if ($dateTo >= $dateFrom) {
            $arrayDateFrom = explode('-', $dateFrom);
            $i  = 0;
            $mm = $arrayDateFrom[1];
            $yy = $arrayDateFrom[0];
            
            while (true) {
                $months[$i]['start'] = $this->family->getStartMonthDate($yy, $mm);
                $months[$i]['end']   = $this->family->getEndMonthDate($yy, $mm);
                $response[$i] = array(DateHelper::getMonthName($mm), 0);
                $i++;
                
                if ($yy.'-'.$mm == $dateTo) {
                    break;
                } else {
                    if ($mm == 12) {
                       $mm  = 1;
                       $yy += 1;
                    } else {
                        $mm += 1;
                    }
                    if($mm < 10) {
                        $mm = '0'.$mm;
                    }
                }
            }
        }
        
        $count = count($months);
        foreach ($incomes AS $income) {  
            for ($i=0; $i<$count; $i++) {
                if ($income['date_created'] >= $months[$i]['start'] && $income['date_created'] <= $months[$i]['end']) {
                    break;
                }
            }
            
            $response[$i][1] = $response[$i][1] + $income['amount'];
        }
        
        return $response;
    }
    
    public function getDataForFamilyToArray(Family $family, $page, $limit, $sidx, $sord, $dateTo = null, $dateFrom = null)
    {
        if (!$sidx) {
            $sidx = 1;
        }

        $dateFrom = FamilyHelper::getStartMonth($family, $dateFrom);
        $dateTo   = FamilyHelper::getEndMonth($family, $dateTo);
          
        $qb = $this->getEntityManager()->getRepository('BudgetBudgetBundle:Income')->createQueryBuilder('i')
                   ->select('i')
                   ->where('i.family = :family')
                   ->andWhere('i.date_deleted IS NULL')
                   ->andWhere('i.date_created >= :dateFrom')
                   ->andWhere('i.date_created <= :dateTo')
                   ->addOrderBy($sidx , $sord)
                   ->setParameter('family' , $family->getId())
                   ->setParameter('dateTo' , $dateTo)
                   ->setParameter('dateFrom' , $dateFrom);
 
        return  $qb->getQuery()->getArrayResult(); 
    }  
}